import connector.driver
from connector.driver import DatabaseDriver
# QMessageBox是Qt GUI库中的一个类，用于创建消息框，可以用来向用户显示信息、警告或错误消息


# 用户注册
def register_user(driver: DatabaseDriver, username: str, password: str,
                  role: str):
    # 从数据库判断是否有重名的用户
    result: tuple = driver.query_one(
        "select user_id from user where user_name = '{}'".format(username), log=True)
    if result is None:
        update: int = driver.insert(
            "insert into user(user_name,user_password,user_role) values ('{}','{}','{}')"
            .format(username, password, role), log=True)
        return update


# 用户登录
def login(driver: DatabaseDriver, username: str, password: str) -> tuple:
    # 根据用户名和密码查询是否有符合的用户
    result: tuple = driver.query_one("select user_id,user_role from user where "
                                     "user_name = '{}' and user_password='{}'"
                                     .format(username, password), log=True)
    return result


if __name__ == "__main__":
    driver = connector.driver.DatabaseDriver()
    driver.close()
